import {ConfigKey} from "../../../script/constant/ConfigConstant";
import {TaskApplyData, TypeTaskRet} from "../../../script/type/Type";
import {TaskController} from "../../../script/controller/TaskController";
import {DataRouter} from "../../../script/frame/dataSource/DataRouter";
import {
    ConditionData,
    EqualType,
    ItemData,
    TaskData,
    TaskType,
    UnitData
} from "../../../script/frame/dataSource/DataModel";
import {DataRouterUtil} from "../../../script/utils/router/DataRouterUtil";
import Configer from "../../../script/frame/apiModel/config/Configer";
import {UIConfigKey} from "../../../script/constant/UIConfig";
import * as React from "../../../script/frame/apiModel/gui/React";
import {SyncId, UISyncData, View} from "../../../script/frame/apiModel/gui/React";
import {GuiKeys} from "../../../script/controller/model/ControllerOpt";
import {TaskUtil} from "../../../script/controller/util/TaskUtil";
import {Logger} from "../../../script/frame/apiModel/config/Logger";
import {ResourceKey} from "../../../script/constant/ResourceConstant";
import {CodeUtil} from "../../../script/frame/codeAdapter/CodeUtil";
import {publicGui} from "../../interface/PublicGui";
import {LightButton} from "../component/LightButton";

export interface IProp {
    taskDatas: TypeTaskRet;
    taskApplyDatas: { [taskId: string]: TaskApplyData };
    hidden: boolean;
}
export interface IState {

}

export default class TaskBar extends React.Element<IProp, IState> {

    render = () => {
        const backSize = { h: 0, w: 0.12 };

        const taskButtonY = Configer.getUIConfig(UIConfigKey.taskButtonY)

        const backPos = { x: 1 - backSize.w, y: taskButtonY }
        const fontSize = 0.015;
        const nameFontSize = 0.018
        const taskItemImage = Configer.getResource(ResourceKey.taskItemImage) as string;
        const mainTaskLogo = Configer.getResource(ResourceKey.mainTaskLogo) as string;
        const deputyTaskLogo = Configer.getResource(ResourceKey.deputyTaskLogo) as string;

        const circleCloseButton = Configer.getResource(ResourceKey.circleCloseButton) as string;
        const unitUseFamily = Configer.getConfig(ConfigKey.unitUseFamily) as number;
        const itemUseFamily = Configer.getConfig(ConfigKey.itemUseFamily) as number;

        const taskButtonTransImage = Configer.getResource(ResourceKey.taskButtonTransImage) as string;
        let allHidden = true;

        const fun = (taskDatas: TaskData[], taskType: TaskType, taskNum: number) => {
            const renderDatas: any[] = [];
            let hidden = true;
            for (let i = 0; i < taskNum; i++) {
                const tempTask = taskDatas && taskDatas[i];
                if (tempTask) {
                    renderDatas.push(tempTask)
                    hidden = false;
                    allHidden = false;
                } else {
                    renderDatas.push('')
                }
            }

            const itemsSize = { w: 0.12500, h: 0 };
            const buttonSize = { w: 0.05, h: 0.03 };
            const buttonMarginL = itemsSize.w / 2 - buttonSize.w / 2

            const eles = renderDatas.map(
                (taskData, index, array) => {
                    const currentTask: TaskData = taskData;
                    let retStr = '';
                    const taskId = currentTask?.id;
                    const showCondition = currentTask?.showCondition;
                    if (!CodeUtil.isNullOrZero(taskId)) {
                        let logoImage;
                        let typeStr;
                        switch (taskType) {
                            case TaskType.main: {
                                typeStr = '[主线]';
                                break;
                            }
                            case TaskType.deputy: {
                                typeStr = '[支线]';
                                break;
                            }
                            case TaskType.team: {
                                typeStr = '[团队]';
                                break;
                            }
                            case TaskType.circulate: {
                                typeStr = '[循环]';
                                break;
                            }
                        }
                        let hiddenLoseButton = false;
                        if (taskType === TaskType.main || taskType === TaskType.team) {
                            logoImage = mainTaskLogo;
                            hiddenLoseButton = true;
                        } else {
                            logoImage = deputyTaskLogo;
                        }
                        const iconSize = { w: 0.01, h: 0.02 };
                        const name = `${typeStr} ${currentTask.task_name}`;
                        const nameSize = { w: itemsSize.w, h: nameFontSize };
                        if (!CodeUtil.isNullOrZero(currentTask.task_content)) {
                            // const getContent = `\n${taskData.task_content}`;
                            const getContent = `${currentTask.task_content}`;
                            retStr = retStr + getContent;
                        }
                        const conditionDatas = DataRouterUtil.getConditionDataMapByTaskId(taskId);
                        if (showCondition && showCondition > 0 && conditionDatas) {
                            conditionDatas.forEach(
                                (datas, index, array) => {
                                    for (let i = 0; i < datas.length; i++) {
                                        const data = datas[i];
                                        let str = this.getStrFun(data, taskId);
                                        if (i < datas.length - 1) {
                                            str = str + '\n或\n';
                                        }
                                        retStr = retStr + str;
                                    }
                                }
                            )
                        }

                        if (!CodeUtil.isNullOrZero(currentTask.task_reward)) {
                            const task_reward = `${currentTask.task_reward}`;
                            retStr = retStr + '\n' + task_reward;
                        }

                        const itemSize = { w: 0.125, h: 0.0083 };

                        const retLineArr = retStr.split('\n');
                        const retLineNum = retLineArr.length + 1;

                        const contentSize = { w: itemSize.w, h: retLineNum * fontSize };

                        const nameMargin = 0.00498;
                        const contentMargin = 0.0083;
                        const buttonMarginT = 0.003;

                        itemSize.h = itemSize.h + nameSize.h +  contentSize.h + buttonSize.h + nameMargin + contentMargin + buttonMarginT;

                        itemsSize.h = itemsSize.h + itemSize.h;

                        return <View   key={`${GuiKeys.taskBarItem}|${taskType}|${index}`} style={{ width: itemSize.w, height: itemSize.h, paddingL: 0.005 }}>
                            <View key={`${GuiKeys.taskBarItemName}|${taskType}|${index}`} style={{ width: nameSize.w, height: nameSize.h, marginT: nameMargin, marginL: 0.00125, fontSize: nameFontSize }}>{name}</View>
                            <View key={`${GuiKeys.taskBarItemContent}|${taskType}|${index}`} style={{ width: contentSize.w, height: contentSize.h, marginT: contentMargin, fontSize: fontSize }}>{retStr}</View>
                            <LightButton   key={`${GuiKeys.taskBarItemCloseButton}|${taskType}|${index}`} onMouseDown={() => { return this.loseTask(taskId)} } style={{ hidden: hiddenLoseButton, backgroundImage: circleCloseButton, width: iconSize.w, height: iconSize.h, float: true, marginT: nameMargin, marginL: 0.1 }}></LightButton>
                            <LightButton key={`${GuiKeys.taskBarItemModel}|${taskType}|${index}`} onMouseDown={() => { return this.toToaskTask(taskId)} } style={{ backgroundImage: taskButtonTransImage, width: buttonSize.w, height: buttonSize.h, marginL: buttonMarginL, marginT: buttonMarginT }}></LightButton>
                        </View>
                    }
                    return <View   key={`${GuiKeys.taskBarItem}|${taskType}|${index}`} style={{ hidden: true }}>
                        <View   key={`${GuiKeys.taskBarItemName}|${taskType}|${index}`} style={{ hidden: true }}></View>
                        <View   key={`${GuiKeys.taskBarItemContent}|${taskType}|${index}`} style={{ hidden: true }}></View>
                        <LightButton   key={`${GuiKeys.taskBarItemCloseButton}|${taskType}|${index}`} style={{ hidden: true }}></LightButton>
                        <LightButton   key={`${GuiKeys.taskBarItemModel}|${taskType}|${index}`} style={{ hidden: true }}></LightButton>
                    </View>
                }
            )
            backSize.h = backSize.h + itemsSize.h;
            return <View   key={`${GuiKeys.taskBarItem}|${taskType}`} style={{ hidden: hidden, width: itemsSize.w, height: itemsSize.h }}>
                {eles}
            </View>
        }


        const mainTaskDatas = this.props.taskDatas?.mainTask;
        const mainNum = Configer.getConfig(ConfigKey.maxMainTask);
        const mainElement: React.Element<any, any> = fun(mainTaskDatas, TaskType.main, mainNum);

        const teamTaskDatas = this.props.taskDatas?.teamTask;
        const teamNum = Configer.getConfig(ConfigKey.maxTeamTask);
        const teamElement: React.Element<any, any>  = fun(teamTaskDatas, TaskType.team, teamNum);

        const deputyTaskDatas = this.props.taskDatas?.deputyTask;
        const circulateTaskDatas = this.props.taskDatas?.circulateTask;
        const deputyNum = Configer.getConfig(ConfigKey.maxDeputyTask);
        const circulateNum = Configer.getConfig(ConfigKey.maxCirculateTask);
        const sumTaskDatas: TaskData[] = [];
        if (deputyTaskDatas) {
            sumTaskDatas.push(...deputyTaskDatas);
        }
        if (circulateTaskDatas) {
            sumTaskDatas.push(...circulateTaskDatas);
        }
        const sumElement: React.Element<any, any>  = fun(sumTaskDatas, TaskType.deputy, circulateNum + deputyNum);

        let hidden = this.props.hidden;

        if (hidden || allHidden) {
            hidden = true;
        }

        return <View   key={GuiKeys.taskBarCon} style={{ hidden: hidden, backgroundImage: taskItemImage, width: backSize.w, height: backSize.h, top: backPos.y, left: backPos.x }}>
            {mainElement}
            {teamElement}
            {sumElement}
        </View>
    }


    getStrFun = (data: ConditionData, taskId: string) => {
        const recordId = data.value;
        const conditionData = data;
        const equalType = conditionData.equalType;
        let sumNum = 0;
        if (conditionData.num) {
            sumNum = conditionData.num;
        }
        if (conditionData) {
            switch (equalType) {
                case EqualType.equal:{
                    sumNum = sumNum;
                    break;
                }
                case EqualType.greaterThan:{
                    sumNum = sumNum + 1;
                    break;
                }
            }
        }

        let unitName;
        const unitData = DataRouter.getUnitDataByUnitId(recordId);
        const itemData = DataRouter.getItemDataByItemId(recordId);

        if (unitData) {
            unitName = unitData?.Name;
        } else if (itemData) {
            unitName = itemData?.Name;
        } else {
            unitName = recordId;
        }


        let recordNum = TaskUtil.getRecordNum(this.props.taskApplyDatas, taskId, recordId);
        if (recordNum > sumNum) {
            recordNum = sumNum
        }
        const tempStr = `\n${unitName}: ${recordNum} / ${sumNum}`;

        return tempStr;
    }

    loseTask = (taskId: string) => {
        const fun = () => {
            const syncData: UISyncData = { id: SyncId.loseTask, param: { taskId: taskId } };
            return syncData;
        }
        publicGui.showDialogPage(0.11, ' ', undefined, ResourceKey.dialogDeleteTaskBoard, ResourceKey.dialogDeleteTaskOk, ResourceKey.dialogDeleteTaskCanel, fun, undefined, { w: 0.025, h: 0.02 });
    }

    toToaskTask = (taskId: string) => {
        const syncData: UISyncData = { id: SyncId.toastTask, param: { taskId: taskId } };
        return syncData;
    }

}